import axios from "axios";
import store from "../store";

const method = {};
method.install = function(Vue, options) {
  Vue.prototype.$exportFile = function(objectUrl) {
    const a = document.createElement("a");
    a.setAttribute("href", objectUrl);
    a.click();
  };
  Vue.prototype.$exportFilePost = function(url, data, name) {
    axios({
      url,
      data,
      method: "post",
      responseType: "blob",
      headers: {
        token: store.state.auth.token,
        "Content-Type": "application/json"
      }
    })
      .then(res => {
        const link = document.createElement("a");
        const blob = new Blob([res.data], {
          type: "application/vnd.ms-excel"
        });
        link.style.display = "none";
        link.href = URL.createObjectURL(blob);
        link.setAttribute("download", `${store.state.hr.companyName}_` + name + ".xls");
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      })
      .catch(err => {
        console.log("debug log --> ", err);
      });
  };
};

export default method;
